<?php
function scandirFolder($path){
    $list = [];
    $temp_list = scandir($path);
    foreach ($temp_list as $file){
        if ($file != ".." && $file != "."){
            if (is_dir($path . "/" . $file)){
                //子文件夹，进行递归
                $list[][$file] = scandirFolder($path . "/" . $file);

            }else{
                //根目录下的文件
                $list[] = $file;
            }

        }
    }
    return $list;
}
function scan_dir($dir = '/www/wwwroot/public/', $filter = array())
{

    if (!is_dir($dir)) return false;
    $files = array_diff(scandir($dir), array('.', '..'));
    if (is_array($files)) {
        foreach ($files as $key => $value) {
            if (is_dir($dir . '/' . $value)) {
                $files[$value] = scan_dir($dir . '/' . $value, $filter);
                unset($files[$key]);
                continue;
            }
            $pathinfo = pathinfo($dir . '/' . $value);
            $extension = array_key_exists('extension', $pathinfo) ? $pathinfo['extension'] : '';
            if (!empty($filter) && !in_array($extension, $filter)) {
                unset($files[$key]);
            }
        }
    }
    unset($key, $value);

    return $files;
}

function uploadOne($img = 'picUrl', $type = 'thumb')
{
    $file = request()->file($img);
    writeLogPic($file);
    if (!empty($file)) {

        // 移动到框架应用根目录/public/uploads/ 目录下
        $upload = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        writeLogPic('upload:'.($upload));
        // halt($upload);
        if ($upload) {

            if ($type == 'thumb') {
                //图片处理
                //imgDispose('public' . DS . 'uploads' . DS . $upload->getSaveName());
            }

            //返回图片地址
            writeLogPic('ret:'.'uploads/' . date('Ymd') . '/' . $upload->getFilename());
            return 'uploads/' . date('Ymd') . '/' . $upload->getFilename();
        }
        writeLogPic('getError:'.$file->getError());
        return $file->getError();
    }

    return request()->param($img,'');
}

function uploadOne1($img = 'picUrl', $type = 'thumb')
{
    $file = request()->file($img);

    if (!empty($file)) {

        // 移动到框架应用根目录/public/uploads/ 目录下
        $upload = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

        // halt($upload);
        if ($upload) {

            if ($type == 'thumb') {
                //图片处理
                imgDispose('public' . DS . 'uploads' . DS . $upload->getSaveName());
            }

            //返回图片地址
            return 'uploads/' . date('Ymd') . '/' . $upload->getFilename();
        }
        return $file->getError();
    }
    return request()->param($img.'_url','');
}

function getDistance($lat1, $lng1, $lat2, $lng2){

    // 将角度转为狐度
    $radLat1 = deg2rad($lat1);// deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);

    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;

    $s = 2 * asin(sqrt(pow(sin($a / 2), 2)+cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;

    return $s;

}

